<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Index</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/index.css">
    <script src="js/jquery-3.3.1.min.js"></script>
</head>
<body>
    <div class="container-fluid">
        <!-- header-keyword -->
        <div class="row" id="header">
            <div class="col-md-10">
                <input type="text" class="form-control" id="keyword" placeholder="keyword">
            </div>
            <div class="col-md-2">
                <button type="button" class="btn btn-info">Search</button>
            </div>
        </div>
        <!-- nav -->
        <div class="row">
            <div class="col-md-6"></div>
            <div class="col-md-6" align="right" >
                <button type="button" id="btn_login2" class="btn btn-link btn-sm">Sign in</button>
                <button type="button" id="btn_register2" class="btn btn-link btn-sm">Register</button>
                <button class="btn btn-info btn-sm" id="btn_cart" type="button">
                    Cart <span class="badge" id="cartnum">0</span>
                </button>
            </div>
        </div>

        <!-- main -->
        <div class="row" id="main">
            <div class="col-md-12">
                <!-- login -->
                <div id="login">
                    <div class="col-md-12">
                        US BOOKSHOP - LOGIN
                    </div>
                    <div class="col-md-12">
                        <input type="text" class="form-control" id="username" placeholder="Username">
                    </div>
                    <div class="col-md-12">
                        <input type="password" class="form-control" id="password" placeholder="Password">
                    </div>
                    <div class="col-md-12" align="left">
                        <button type="button" class="btn btn-danger" id="btn_login">SUBMIT</button>
                        <button type="button" class="btn btn-danger" id="btn_register">CREATE</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(function () {
            //API URL
            var apiurl="http://localhost:8080";

            initdata();
            //initdata
            function initdata(){
                var book_cart=sessionStorage.getItem("book_cart");
                if(book_cart==null || book_cart==undefined){
                    $("#cartnum").text(0);
                }else{
                    book_cart=JSON.parse(book_cart);
                    $("#cartnum").text(book_cart.length);
                }
            }

            //Login
            $("#btn_login").click(function(){
                var username=$("#username").val();
                var password=$("#password").val();
                if(username=="" || password==""){
                    alert("Please do not leavethe fields empty");
                    return false;
                }
                var params={user_name:username,user_pass:password};
                $.ajax({
                    url: apiurl+"/Login.php",
                    type: "POST",
                    dataType: "json",
                    data: params,
                    success: function (data) {
                        console.log(data);
                        if(data.code==0){
                            //save location
                            sessionStorage.setItem("book_user", JSON.stringify(params));
                            window.location.href="index.html";
                        }else{
                            $($("body")[0]).html('<h1 style="text-align:center;color:red;">Invalid login，please login again.</h1>');
                            setInterval(() => {
                                window.location.href="login.html";
                            }, 3000);
                        }
                    }
                });
            });

            //Login
            $("#btn_login2").click(function(){
                window.location.href="login.html";
            });
         
            $("#btn_register").click(function(){
                window.location.href="createaccount.html";
            });

            //btn_register
            $("#btn_register2").click(function(){
                window.location.href="createaccount.html";
            });

            $("#btn_cart").click(function(){
                window.location.href="mycart.html";
            });

            
        })
    </script>
</body>
</html>